/**
 * 后台基础JS lib
 * @author YAOYOA
 */
//正则
var REG_EMAIL=/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z0-9]{2,10}([\.][a-z]{2,3})?/i;
var REG_MOBILE=/^1[3,4,5,8]{1}[0-9]{1}[0-9]{8}$/;
var GET_ORDER_DETAIL_URL='/ctr/ajax/ajax_show_order_detial/';
var NO_IMG_URL='/public/ctr/images/no_pic.jpg';
//YY_dialog地址
var YY_DIALOG_URL='/public/common/js/YY_dialog.js';
/**
 * 根据商品编号和属性编号生成商品url
 * @param p pid
 * @param a	attr_id
 * @returns {String}
 */
var get_product_url=function(p,a){var url='http://www.max1234567.com/product/'+p;if (typeof a!='undefined'&&a>0){url+='/'+a;}return url+='.html';};

/**
 * catalog_select ajax获取商品分类
 */
function catalog_select(main,sub,branch,generate,all){
	if (typeof generate=='undefined'||generate||generate==null)
		document.write('<select name="cat_main" class="catalog_main"></select><select name="cat_sub" class="catalog_sub"></select><select name="cat_branch" class="catalog_branch"></select>');
	var catalog_main_str='',catalog_main_id=0,catalog_sub_id=1,catalog_sub_str='',catalog_branch_id=1,catalog_branch_str='',q_str='';
	if (typeof main!='undefined')catalog_main_id=main;
	if (typeof sub!='undefined')catalog_sub_id=sub;
	if (typeof branch!='undefined')catalog_branch_id=branch;
	if (typeof all=='undefined')all=true;
	$('select.catalog_main,select.catalog_sub').unbind();
	if (!all)q_str='is_display=1&';
	$.ajax({
		url:'/ctr/ajax/get_sub_cat',
		data:q_str,
		success: function(data){
			catalog_main_str='';
			$.each(data,function(key,val){catalog_main_str+='<option value="'+val.cat_id+'"'+(catalog_main_id==val.cat_id?' selected="selected"':'')+'>'+val.cat_name+'</option>';});
			$('select.catalog_main').html('<option selected="selected" value="0">请选择分类</option>'+catalog_main_str);
			if ($('select.catalog_main option:selected').val()>0)$('select.catalog_main').change();
		},
		dataType: 'json',
		type: "POST"
	});
	$('select.catalog_main').live('change',function(){
		var obj=$(this);
		if (obj.children('option:selected').val()!='0'&&typeof obj.children('option:selected').val()!='undefined')
			$.ajax({
				url:'/ctr/ajax/get_sub_cat',
				data: q_str+'parent_id='+obj.val(),
				success: function(data){
					if (data!=null&&data.length>0){
						var initialize_sub=false;
						catalog_sub_str='';
						$.each(data,function(key,val){
							catalog_sub_str+='<option value="'+val.cat_id+'"'+(catalog_sub_id==val.cat_id?' selected="selected"':'')+'>'+val.cat_name+'</option>';
							if (catalog_sub_id==val.cat_id)initialize_sub=true;
						});
						obj.siblings('select.catalog_sub').html('<option selected="selected" value="0">请选择分类</option>'+catalog_sub_str).show();
						if (!initialize_sub)
							obj.siblings('select.catalog_branch').empty().hide();
						else
							obj.siblings('select.catalog_sub').change();
					}else{
						obj.siblings('select.catalog_sub').empty().show();
						obj.siblings('select.catalog_branch').empty().hide();
					}
				},
				dataType: 'json',
				type: "POST"
			});
		else{
			obj.siblings('select.catalog_sub').empty().show();
			obj.siblings('select.catalog_branch').empty().hide();
		}
	});
	$('select.catalog_sub').live('change',function(){
		var obj=$(this);
		if (obj.children('option:selected').val()!='0'&&typeof obj.children('option:selected').val()!='undefined')
			$.ajax({
				url:'/ctr/ajax/get_sub_cat',
				data: q_str+'parent_id='+obj.val(),
				success: function(data){
					if (data!=null&&data.length>0){
						catalog_branch_str='';
						$.each(data,function(key,val){catalog_branch_str+='<option sku="'+val.cat_sku+'" value="'+val.cat_id+'"'+(catalog_branch_id==val.cat_id?' selected="selected"':'')+'>'+val.cat_name+'</option>';});
						obj.siblings('select.catalog_branch').html('<option selected="selected" value="0">请选择分类</option>'+catalog_branch_str).show();
					}else{
						obj.siblings('select.catalog_branch').empty().show();
					}
				},
				dataType: 'json',
				type: "POST"
			});
		else{
			obj.siblings('select.catalog_branch').empty().show();
		}
	});
	setTimeout(function(){$('select.catalog_main').change();$('select.catalog_sub').empty().hide();$('select.catalog_branch').empty().hide();},1);
}
/**
 * 省市区联动
 */
function direction_select(p,c,d,generate){
//	alert(p+'\n'+c+'\n'+d);
	if (typeof generate=='undefined'||generate)
		document.write('<select name="province" class="province"></select><select name="city" class="city"></select><select name="district" class="district"></select>');
	var province_str='',province_id=2,mdc_id=0,city_id=0,city_str='',district_id=0,district_str='',mdc_str='';
	if (typeof p!='undefined'&&p!=null&&p!=='')province_id=p;
	if (typeof c!='undefined'&&c!=null&&c!=='')city_id=c;
	if (typeof d!='undefined'&&d!=null&&d!=='')district_id=d;
	$.each(province,function(key,val){var province_array=val.split("|");province_str+='<option value="'+province_array[0]+'"'+(province_id==province_array[0]?' selected="selected"':'')+'>'+province_array[1]+'</option>';});
	$('select.province').html(province_str).change(function(){
		var city_str='';
		$.each(city[parseInt($('select.province option').index($('select.province option:selected')))],function(key,val){
			var city_array=val.split("|");
			city_str+='<option value="'+city_array[0]+'"'+(city_id==city_array[0]?' selected="selected"':'')+'>'+city_array[1]+'</option>';
		});
		$(this).siblings('.city').html(city_str).next('.district').empty();
		if (city[parseInt($('select.province option').index($('select.province option:selected')))].length==1&&city[parseInt($('select.province option').index($('select.province option:selected')))][0].split('|')[1]==$(this).find('option:selected').text()){
			var district_str='';
			$.each(area[parseInt($('select.province option').index($('select.province option:selected')))][parseInt($('select.city option').index($('select.city option:selected')))],function(key,val){
				var district_array=val.split("|");
				district_str+='<option value="'+district_array[0]+'"'+(district_id==district_array[0]?' selected="selected"':'')+'>'+district_array[1]+'</option>';
			});
			$(this).siblings('select.city').hide().change().siblings('select.district').html(district_str);
			//alert(district_str);
			
		}
		else{
			$(this).siblings('select.city').show().change();
			$(this).siblings('select.city').change();
		}
		if ($('select.mdc_p') && $('select.mdc_p').attr("disabled")!='disabled' && $('select.mdc_p').attr("disabled")!=true){
			//alert($('select.province').val());
			$('select.mdc_p').val($('select.province').val()).change();
		}
	}).siblings('select.city').change(function(){
		var district_str='';
		if (typeof area[parseInt($('select.province option').index($('select.province option:selected')))][parseInt($('select.city option').index($('select.city option:selected')))]=='undefined')return false;
		$.each(area[parseInt($('select.province option').index($('select.province option:selected')))][parseInt($('select.city option').index($('select.city option:selected')))],function(key,val){
			var district_array=val.split("|");
			district_str+='<option value="'+district_array[0]+'"'+(district_id==district_array[0]?' selected="selected"':'')+'>'+district_array[1]+'</option>';
		});
		$(this).siblings('.district').html(district_str).show();
		if ($('select.mdc_c') && $('select.mdc_p').attr("disabled")!='disabled' && $('select.mdc_p').attr("disabled")!=true){
			$('select.mdc_c').val($('select.city').val()).change();
		}
	}).siblings('select.district').change(function(){
		if ($('select.mdc_d') && $('select.mdc_p').attr("disabled")!='disabled' && $('select.mdc_p').attr("disabled")!=true){
			$('select.mdc_d').val($('select.district').val()).change();
		}
	});
	//alert($('select.province').length);
	setTimeout(function(){$('select.province').val(province_id).change();},2);
}
function mdc_select(p,c,d,m,generate){
	if (typeof generate=='undefined'||generate)
		document.write('<select name="mdc_p" class="mdc_p"></select><select name="mdc_c" class="mdc_c"></select><select name="mdc_d" class="mdc_d"></select><select name="mdc" class="mdc"></select>');
	var province_str='',province_id=1,mdc_id=1,city_id=1,city_str='',district_id=1,mdc_str='';
	if (typeof p!='undefined'&&p!=null)province_id=p;
	if (typeof c!='undefined'&&c!=null)city_id=c;
	if (typeof d!='undefined'&&d!=null)district_id=d;
	if (typeof m!='undefined'&&m!=null)mdc_id=m;
	$.ajax({
		url:'/ctr/ajax/ajax_region_mdc_province',
		//data:'city_id='+$('select.province').val(),
		success: function(data){
			province_str='';
			$.each(data,function(key,val){province_str+='<option db_id="'+val.db_id+'" value="'+val.province_id+'"'+(province_id==val.province_id?' selected="selected"':'')+'>'+val.alias+'</option>';});
			$('select.mdc_p').html(province_str);
			if ($('select.mdc_p option:selected').val()>0)$('select.mdc_p').change();
		},
		dataType: 'json',
		type: "POST"
	});
	$('select.mdc_p').change(function(){
		if ($('select.mdc_p option:selected').val()!='0'&&typeof $('select.mdc_p option:selected').val()!='undefined')
			$.ajax({
				url:'/ctr/ajax/ajax_region_mdc_city',
				data: 'province_id='+$('select.mdc_p').val(),
				success: function(data){
					if (data!=null&&data.length>0){
						var initialize_sub=false;
						city_str='';
						$.each(data,function(key,val){
							city_str+='<option value="'+val.city_mdc_id+'"'+(city_id==val.city_mdc_id?' selected="selected"':'')+'>'+val.alias+'</option>';
							if (city_id==val.city_mdc_id)initialize_sub=true;
						});
						$('select.mdc_c').html(city_str).show().change();
						if (!initialize_sub){
							$('select.mdc_d').empty().hide();
							$('select.mdc').empty().hide();
						}
						//else
							//$('select.mdc_c').change();
					}else{
						$('select.mdc_c').empty().hide();
						$('select.mdc_d').empty().hide();
						$('select.mdc').empty().hide();
					}
				},
				dataType: 'json',
				type: "POST"
			});
		else{
			$('select.mdc_c').empty().hide();
			$('select.mdc_d').empty().hide();
			$('select.mdc').empty().hide();
		}
	});
	$('select.mdc_c').change(function(){
		if ($('select.mdc_c').val()!='0'&&typeof $('select.mdc_c').val()!='undefined')
			$.ajax({
				url:'/ctr/ajax/ajax_region_mdc_region',
				data: 'city_mdc_id='+$('select.mdc_c').val(),
				success: function(data){
					if (data!=null&&data.length>0){
						var initialize_sub=false;
						district_str='';
						
						$.each(data,function(key,val){
							district_str+='<option value="'+val.region_mdc_id+'"'+(district_id==val.region_mdc_id?' selected="selected"':'')+'>'+val.alias+'</option>';
							if (district_id==val.region_mdc_id)initialize_sub=true;
						});
						$('select.mdc_d').html(district_str).show().change();
						$('select.mdc').empty().hide();
						if (!initialize_sub){
							$('select.mdc').empty().hide();
						}
//						else
//							$('select.mdc_d').change();
					}else{
						$('select.mdc_d').empty().hide();
					}
				},
				dataType: 'json',
				type: "POST"
			});
		else{
			$('select.mdc_d').empty().hide();
			$('select.mdc').empty().hide();
		}
	});
	$('select.mdc_d').change(function(){
		if ($('select.mdc_d').val()!='0'&&typeof $('select.mdc_d').val()!='undefined')
			$.ajax({
				url:'/ctr/ajax/ajax_region_mdc_mdc',
				data: 'region_mdc_id='+$('select.mdc_d').val(),
				success: function(data){
					if (data!=null&&data.length>0){
						mdc_str='';
						$.each(data,function(key,val){mdc_str+='<option value="'+val.mdc_id+'"'+(mdc_id==val.mdc_id?' selected="selected"':'')+'>'+val.mdc_name+'</option>';});
						$('select.mdc').html(mdc_str).show();
					}else{
						$('select.mdc').empty().hide();
					}
				},
				dataType: 'json',
				type: "POST"
			});
		else
			$('select.mdc').empty().hide();
	});
	setTimeout(function(){$('select.mdc_p').change();},1);
}
var ajaxlist = new Object;
$(document).ready(function() {
	//	左侧菜单列表显示与隐藏
	if ($.cookie("hide_list" )== "show")
	{
		$("#show_list").css("display","none");
		$("#page_left_menu").css("display","");
		$("#page_right_content_ctn").css("margin-left","176px");
	}
	else if ($.cookie("hide_list") == "hide")
	{
		$("#page_left_menu").css("display","none");
		$("#show_list").css("display","block");
		$("#page_right_content_ctn").css("margin-left","18px");
	}
	$('#hide_list').click(function(){
		$("#page_left_menu").css("display","none");
		$("#show_list").css("display","block");
		$("#page_right_content_ctn").css("margin-left","18px");
		$.cookie("hide_list","hide");
	});
	$('#show_list').click(function(){
		$("#show_list").css("display","none");
		$("#page_left_menu").css("display","");
		$("#page_right_content_ctn").css("margin-left","176px");
		$.cookie("hide_list","show");
	});
	
	//数量输入框基础校验
	$("input.numeric,input.money,input.discount").css({'ime-mode':'disabled'});
	$("input.numeric").live('keydown',function(e){
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		sk = e.shiftKey?e.shiftKey:((keyCode == 16)?true:false);
		if (sk)return false;
		if(!(keyCode==46)&&!(keyCode==8)&&!(keyCode==9)&&!(keyCode==37)&&!(keyCode==39))
			if(!((keyCode>=48&&keyCode<=57)||(keyCode>=96&&keyCode<=105)))
			 return false;
	}).live('blur',function(event){
		$(this).val(Math.round(this.value));
		if ($(this).val()<1)$(this).val(1);
		if ($(this).val()>99999)$(this).val(99999);
	}).live('keyup',function(){
		if ($(this).val()<1)$(this).val(1);
		if ($(this).val()>99999)$(this).val(99999);
	});
	$("input.money").live('keydown',function(e){
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		sk = e.shiftKey?e.shiftKey:((keyCode == 16)?true:false);
//		$(this).val(keyCode);return false;
		if (sk)return false;
		if(!(keyCode==46)&&!(keyCode==8)&&!(keyCode==9)&&!(keyCode==37)&&!(keyCode==39))
			if(!((keyCode>=48&&keyCode<=57)||(keyCode>=96&&keyCode<=105)||keyCode==110||keyCode==190))
				return false;
	}).live('blur',function(event){
		$(this).val(fmoney(this.value,2,false));
		if ($(this).val()<0)$(this).val(0);
		//if ($(this).val()>99999)$(this).val(99999);
	}).live('keyup',function(){
		if ($(this).val()<0)$(this).val(0);
		//if ($(this).val()>99999)$(this).val(99999);
	});
	$("input.discount").live('keydown',function(e){
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		sk = e.shiftKey?e.shiftKey:((keyCode == 16)?true:false);
		if (sk)return false;
		if(!(keyCode==46)&&!(keyCode==8)&&!(keyCode==9)&&!(keyCode==37)&&!(keyCode==39))
			if(!((keyCode>=48&&keyCode<=57)||(keyCode>=96&&keyCode<=105)||keyCode==110||keyCode==190))
				return false;
	}).live('blur',function(event){
		$(this).val(fmoney(this.value));
		if ($(this).val()<0)$(this).val(0);
		if ($(this).val()>1)$(this).val(1);
	}).live('keyup',function(){
		if ($(this).val()<0)$(this).val(0);
		if ($(this).val()>1)$(this).val(1);
	});
	//在页面ajax调用的时候自动显示loading图片
	$('<img/>').attr({
		'src': '/public/common/images/loading_b.gif', 
		'alt': '载入中'
	}).css({position:'absolute',top:30,left:($('body').width()-32)/2})
	.appendTo($('body')).hide().ajaxStart(function(){
		$(this).show();
	}).ajaxStop(function(){
		$(this).hide();
	});
	//文本框在获得焦点后可以自动清空默认提示语
	$('input[type!="hidden"][title!=""]').each(function(){
		if ($(this).val()==''&&$(this).attr('title')!=''){
			$(this).val($(this).attr('title'));
		}
		$(this).focus(function(){
			if ($(this).attr('title')==$(this).val())
				$(this).val('');
			$(this).blur(function(){
				if ($(this).val()=='')
					$(this).val($(this).attr('title'));
				
			});
		});
	});
	//页面右下角提示框
	if($('#page_notice_ctn')){
		$('#page_notice_ctn h4 b').live('click',function(){
			var obj=$('#page_notice_ctn');
			var total_height=$('#page_notice_ctn ul').height()+39;
			if (!obj.hasClass('open')){
				obj.addClass('open');
				obj.animate({
					height: total_height,
					top:document.documentElement.clientHeight + document.documentElement.scrollTop + document.body.scrollTop-total_height-4
				}, 500, function() {
					obj.height(total_height);
				});
			}else{
				close_notice();
			}
			
			if (obj.height()==29){}
		});
		$('#page_notice_ctn h4 .x').live('click',function(){
			close_notice();
			//show_info('err1！','ok',true,end_action);
			//$('#page_notice_ctn').hide();
		});
		var close_notice=function(){
			$('#page_notice_ctn').animate({
				height: 29,
				top:document.documentElement.clientHeight + document.documentElement.scrollTop + document.body.scrollTop-33
			}, 500, function() {
				$('#page_notice_ctn').height(29);
			}).removeClass('open');
		};
		window.onscroll =function(e){
			var top=document.documentElement.clientHeight + document.documentElement.scrollTop + document.body.scrollTop-$('#page_notice_ctn').height()-4;
			$('#page_notice_ctn').css({top:top});
		};
		$('#page_notice_ctn').css({top:document.documentElement.clientHeight + document.documentElement.scrollTop + document.body.scrollTop-$('#page_notice_ctn').height()-4}).show();
	}
	//表格绑定隔行背景色
	$('table.result:not([class*="no_bg"]) tbody').find("tr:odd").css("background-color", "#fafafa");
});


//--------------- 确定返回----------------------------
function ConfirmDel(str)
{
   if(confirm(str))
     return true;
   else
     return false; 
}
/**
 * 格式化浮点数为金额
 * @param s 浮点数
 * @param n 小数位数
 * @returns {String}
 */
function fmoney(s,n,d){
	if (s=='')return 0;
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	d=typeof d=='undefined'?true:false;
	var l = s.split(".")[0].split("").reverse(),  
	r = s.split(".")[1];
	t = "";
	if (d){
		for(i = 0; i < l.length; i ++ )  
		{  
			t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
		}
	}else{
		for(i = 0; i < l.length; i ++ )  
		{  
			t += l[i] ;  
		}
	}
	return t.split("").reverse().join("") + "." + r;  
}
/**
 * 弹出提示层
 * @param s 字符串 提示层上显示的内容
 */
var show_info_dialog;
/* 弹出自动关闭 */
var c=3;
var h;
var dialog_func;
function timedCount()
{
	$('.btn-bar span').html(+c+"秒后自动关闭");
	c--;
	h=setTimeout(timedCount,1000);
	if(c==0){
	 	clearTimeout(h);//清除
	 	if (typeof dialog_func=='function')setTimeout(dialog_func,10);
	 	show_info_dialog.close_dialog();
	 	c=3;
	} 
}


/* 测试弹出层关闭事件后执行函数 */
var end_action=function(){
};


function show_info(s,t,d,e){
	if (typeof s=='undefined')s='';
	if (typeof t=='undefined')t='box_button';
	if (t=='ok')t='box_button';
	if (t=='err')t='box_button1';


	if (typeof d=='undefined')d='确定';
	if ($('#show_info_dialog').length<=0){
	
		var str='<div id="show_info_dialog" class="box_main"><div class="box_top"><P class="box_top_title">'+
		'<span style="margin-left:20px;"><img src="/public/ctr/images/box_ico.gif" width="18" height="18" /></span><span style="margin-left:5px;">提示</span>'+
		'</P><p class="box_top_close"><img src="/public/ctr/images/box_close.gif" /></p></div>'+
		'<div class="box_middle"><div class='+t+' id="boxbutton"><p class="box_font">'+s+
		'</p></div></div>'+
		'<div class="box_bottom">'+
		'<a class="btn-bar" href="#"><span></span></a>'+
		'</div></div></div>';
	
		$('body').append(str);
		
		//绑定点击事件
		if(typeof e=='function'){
			$(".box_top_close,.btn-bar span").click(e);
		}
	}else{
		$('#show_info_dialog .box_font').html(s);
	}
	$(".box_top_close,.btn-bar span").click(function(){show_info_dialog.close_dialog();});
	if (typeof show_info_dialog=='undefined'){
		var p2={restrict:true,package:false,content_id:"show_info_dialog",force_center:'center'};
		show_info_dialog=new YY_dialog(p2);
	}
	if(d){
		dialog_func=e;
		timedCount();
		$(".box_top_close,.btn-bar span").click( function () {  
			clearTimeout(h);//清除
			c=3;
		});
	}else{
		$('.btn-bar span').html("确定");
	}
	show_info_dialog.show_dialog();
	$('#show_info_dialog')
	if(t=='box_button'){$('#boxbutton').removeClass().addClass("box_button");  }
	if(t=='box_button1'){$('#boxbutton').removeClass().addClass("box_button1");  }
}

document.write('<script src="'+YY_DIALOG_URL+'" language="javascript" type="text/javascript"></script>');/**
 * jquery cookie插件
 */
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options);
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '; path=/';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};